<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="auto">

      <el-form-item label="课程名称" prop="courseId">
        <el-select v-model="queryParams.courseId" placeholder="请选择" clearable>
          <el-option v-for="item in selectCourses" :key="item.courseId" :label="item.courseName"
            :value="item.courseId" />
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="学生手机号" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="缴费状态" prop="priceStatus">
        <el-select v-model="queryParams.priceStatus" placeholder="请选择" clearable>
          <el-option label="未缴费" value="0" />
          <el-option label="已缴费" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="classzList" @selection-change="handleSelectionChange">
      <el-table-column label="学员姓名" align="center" sortable  prop="userName" />
      <el-table-column label="手机号" align="center" sortable  prop="phoneNumber" />
      <el-table-column label="课程名称" align="center" sortable  prop="courseName" />
      <el-table-column label="缴费时间" align="center" sortable  prop="priceTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.priceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费金额" align="center" sortable  prop="amount" />
      <el-table-column label="缴费状态" align="center" sortable  prop="priceStatus">
        <template #default="scope">
          <span>{{ scope.row.priceStatus ? (scope.row.priceStatus === '0' ? '未缴费' : '已缴费') : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['monery:list:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改班级管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="classzRef" class="update-form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="缴费金额" prop="amount">
          <el-input-number v-model="form.amount" :controls="false" :precision="2" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="缴费状态" prop="priceStatus">
          <el-select style="width: 100%;" v-model="form.priceStatus" placeholder="请选择" clearable>
            <el-option label="未缴费" value="0" />
            <el-option label="已缴费" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="缴费时间" prop="priceTime">
          <el-date-picker v-model="form.priceTime" type="date" placeholder="请选择" :size="size" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Classz">
import {
  listMonery,
  updateMonery
} from "@/api/monery";
import { queryCourseList } from '@/api/analyse.js'
const { proxy } = getCurrentInstance();
// 下拉数据列表
const selectCourses = ref([])
// 
const classzList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    courseId: null,
    phoneNumber: null,
    priceStatus: null,
  },
  rules: {
    amount: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    priceStatus: [
      { required: true, message: "不能为空", trigger: "change" }
    ],
    priceTime: [
      { required: true, message: "不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
// 获取下拉
const getSelectList = () => {
  queryCourseList().then(response => {
    if (response.code === 200) {
      selectCourses.value = response.rows
    }
  });
}
/** 查询班级管理列表 */
function getList() {
  loading.value = true;
  listMonery(queryParams.value).then(response => {
    classzList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("classzRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.classId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value.courseId = row.courseId
  form.value.stuId = row.stuId
  form.value.amount = row.amount;
  form.value.priceStatus = '' + row.priceStatus;
  form.value.priceTime = row.priceTime;
  open.value = true;
  title.value = "修改缴费信息";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["classzRef"].validate(valid => {
    if (valid) {
      if (form.value.courseId != null) {
        updateMonery(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('exam/price/export', {
    ...queryParams.value
  }, `缴费_${new Date().getTime()}.xlsx`)
}
getSelectList()
getList();
</script>
<style lang="scss" scoped>
.el-input-number {
  width: 100%;
}

.el-date-picker {
  width: 100%;
}

.update-form {
  :deep(.el-input-number .el-input__inner) {
    text-align: left !important;
  }
}
</style>